/**
 * 
 */
$(document).ready(
	function ()
	{
		$('.sortable').each(
			function (i)
			{
				var table = $(this),
				oTable = table.dataTable({
					aoColumns: [
						{bSortable: false},
						{sType: 'string'},
						{sType: 'string'},
						{bType: 'string'},
						{bType: 'string'},
						{bType: 'string'},
						{sType: 'date'},
						{bSortable: false}
					],
					
					sDom: '<"block-controls"<"controls-buttons"p>>rti<"block-footer clearfix"lf>',
	
					fnDrawCallback: function()
					{
						this.parent().applyTemplateSetup();
					},
	
					fnInitComplete: function()
					{
						this.parent().applyTemplateSetup();
					}
				});
	
				table.find('thead .sort-up').click(
					function(event)
					{
						event.preventDefault();
	
						var column = $(this).closest('th'),
						columnIndex = column.parent().children().index(column.get(0));
	
						oTable.fnSort([[columnIndex, 'asc']]);
	
						return false;
					}
				);
	
				table.find('thead .sort-down').click(
					function(event)
					{
						event.preventDefault();
	
						var column = $(this).closest('th'),
						columnIndex = column.parent().children().index(column.get(0));
	
						oTable.fnSort([[columnIndex, 'desc']]);
	
						return false;
					}
				);
			}
		);
		
		$.fn.updateTabs.enabledHash = false;
	}
);


/**
 * 
 * @param url
 */
function dialog ( url )
{
    var option = {};
	
    option.title	= 'Material';
    option.url	    = url;
    option.minWidth = 600;
    
    option.complete = function () 
    {
    	layoutUpload();
    	
    	$.modal.current.centerModal(); 
    };
    
    option.buttons 	= {
    	'Salvar': function ()
    	{ 
    		$('#form-material').submit();
    	}
    };

    showDialog( option );
}

/**
 * 
 * @returns {Boolean}
 */
function save ()
{
	$('#form-material').removeBlockMessages().blockMessage( 'Aguarde...', {type: 'warning'} );
	
	return true;
}

/**
 * 
 * @param id
 */
function abrir ( id )
{
	if ( !id )
		return false;
	
	var option = {};
	
    option.title	= 'Material';
    option.url	    = baseUrl + '/admin/material/open/id/' + id;
    option.minWidth = 1024;
    option.complete = function() { $.modal.current.centerModal(); };
    
	showDialog( option );
}

/**
 * 
 * @param id
 */
function download ( id )
{
	if ( !id )
		return false;
	
	var url = baseUrl + '/admin/material/download/id/' + id;
	
	window.location.assign( url );
}

/**
 * 
 * @param iframe
 * @returns {Boolean}
 */
function callBackUpload ( iframe )
{
    var div = $(iframe).contents().find('div');
    
    if ( div.length ) {
    	var json = eval('(' + div.eq(0).html() + ')' );
    	
		$(json.description).each(
		    function ( index, value )
		    {
		    	$('#form-material').removeBlockMessages().blockMessage( value.message, {type: value.level} );
		    }
	    );
		
		if ( json.status ) {
			
			history.go(0);
			
		} else {
			
		    if ( json.errors ) {
		    	showErrorsForm( json.errors );
		    	errorUpload('arquivo');
		    }
		    
		}
		
    }
    
    return true;
}